package com.whty.cms.filewps.upload.controller;

import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.whty.cms.filewps.common.message.ResponseMessage;
import com.whty.cms.filewps.upload.dto.TeamworkLesson;
import com.whty.cms.filewps.upload.service.ILessonService;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping(value = {"/teamwork"})
public class TeamworkLessonController {

    private static final Logger logger = LogManager.getLogger(TeamworkLessonController.class);

    @Autowired
    private ILessonService lessonService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage save(@RequestBody JSONObject obj) {
        ResponseMessage result = new ResponseMessage();
        try {
            String objStr = obj.getStr("list");
            List<TeamworkLesson> TeamworkLessonList = JSON.parseArray(objStr, TeamworkLesson.class);
            if (null == TeamworkLessonList || TeamworkLessonList.size() ==0){
                return returnMessage(result, "100000","用户为空","" );
            }
            for (TeamworkLesson teamworkLesson : TeamworkLessonList) {
                if (StringUtils.isBlank(teamworkLesson.getResId()) || StringUtils.isBlank(teamworkLesson.getUserId())) {
                    return returnMessage(result, "100000", "必填参数为空", "");
                }
                String sql = "select id from t_teamwork_lesson where user_id = ? and res_id = ?";
                List<String> list = this.jdbcTemplate.queryForList(sql, String.class, new Object[]{teamworkLesson.getUserId(), teamworkLesson.getResId()});
                teamworkLesson.setCreateTime(new Date());
                if (list != null && !list.isEmpty()) {
                    teamworkLesson.setId(list.get(0));
                    lessonService.updateRes(teamworkLesson);
                } else {
                    teamworkLesson.setId((UUID.randomUUID().toString().replaceAll("-", "")));
                    lessonService.save(teamworkLesson);
                }
            }
            return returnMessage(result, "000000","保存成功","" );
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return returnMessage(result, "400000","系统内部错误",e.getMessage() );
        }
    }

    @RequestMapping(value = "/selectAll", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage selectAll(@RequestBody Map<String,String> map) {
        ResponseMessage result = new ResponseMessage();
        try {
            if(StringUtils.isBlank(map.get("userId")) && StringUtils.isBlank(map.get("resId"))) {
                return returnMessage(result, "100000","用户id或资源为空","" );
            }
            List<TeamworkLesson> list  = lessonService.selectAll(map);
            return returnMessage(result,"000000","成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return returnMessage(result, "400000","系统内部错误",e.getMessage() );
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage updateStatus(@RequestBody JSONObject obj) {
        ResponseMessage result = new ResponseMessage();
        try {
            String resId = obj.getStr("resId");
            if(StringUtils.isBlank(resId)) {
                return returnMessage(result, "100000","资源id为空","" );
            }
            if(StringUtils.isBlank(obj.getStr("type")) && StringUtils.isBlank(obj.getStr("status")) && StringUtils.isBlank(obj.getStr("permission")) && StringUtils.isBlank(obj.getStr("isEdit")) && StringUtils.isBlank(obj.getStr("fid")) && StringUtils.isBlank(obj.getStr("previewUrl")) && StringUtils.isBlank(obj.getStr("downloadUrl")) && StringUtils.isBlank(obj.getStr("title")) ) {
                return returnMessage(result, "100000","必填参数为空","" );
            }
            lessonService.update(obj);
            return returnMessage(result,"000000","成功","");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return returnMessage(result, "400000","系统内部错误",e.getMessage() );
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage delete(@RequestBody TeamworkLesson teamworkLesson) {
        ResponseMessage result = new ResponseMessage();
        try {
            if(StringUtils.isBlank(teamworkLesson.getResId()) || StringUtils.isBlank(teamworkLesson.getUserId())) {
                return returnMessage(result, "100000","必填参数为空","" );
            }
            String sql = "delete from t_teamwork_lesson where user_id =? and res_id=?";
            jdbcTemplate.update(sql, new Object[] { teamworkLesson.getUserId(), teamworkLesson.getResId() });
            return returnMessage(result,"000000","成功","");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return returnMessage(result, "400000","系统内部错误",e.getMessage() );
        }
    }

    private ResponseMessage returnMessage(ResponseMessage result, String code, String msg, Object data) {
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }
}
